package jp.seraph.jsmf.motion;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import jp.seraph.jsade.task.AgentTask;

import org.apache.commons.collections.Predicate;
import org.apache.commons.lang.NullArgumentException;

public class DefaultMotion implements EditableMotion {
    public DefaultMotion() {
        init(new ArrayList<MotionSession>());
    }

    private DefaultMotion(DefaultMotion aBaseMotion){
        init(aBaseMotion.mSessions);
    }

    public DefaultMotion(MotionSession... aSessions) {
        for (MotionSession tPose : aSessions) {
            if (tPose == null)
                throw new NullArgumentException("aSessions");
        }

        List<MotionSession> tSessions = new ArrayList<MotionSession>();
        Collections.addAll(tSessions, aSessions);

        init(tSessions);
    }

    private void init(List<MotionSession> aSessions) {
        mSessions = aSessions;
    }

    private List<MotionSession> mSessions;

    /**
     *
     * @see jp.seraph.jsmf.motion.EditableMotion#addSession(jp.seraph.jsmf.motion.MotionSession)
     */
    public void addSession(MotionSession aSession) {
        if (aSession == null)
            throw new NullArgumentException("aSession");

        mSessions.add(aSession);
    }

    /**
     *
     *
     * @see jp.seraph.jsmf.motion.EditableMotion#removeSession(org.apache.commons.collections.Predicate)
     */
    public MotionSession removeSession(Predicate aCondition) {
        if (aCondition == null)
            throw new NullArgumentException("aCondition");
        
        for (int i = 0; i < mSessions.size(); i++) {
            MotionSession tSession = mSessions.get(i);
            if(aCondition.evaluate(tSession)){
                mSessions.remove(i);
                return tSession;
            }
        }

        return null;
    }

    /**
     *
     * @see jp.seraph.jsmf.motion.EditableMotion#insertSession(int, jp.seraph.jsmf.motion.MotionSession)
     */
    public void insertSession(int aIndex, MotionSession aSession) {
        if (aSession == null)
            throw new NullArgumentException("aSession");
        
        mSessions.add(aIndex, aSession);
    }

    /**
     *
     * @see jp.seraph.jsmf.motion.EditableMotion#removeSession(int)
     */
    public MotionSession removeSession(int index) {
        // TODO 自動生成されたメソッド・スタブ
        return null;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        return new DefaultMotion(this);
    }

    /**
     *
     * @see jp.seraph.jsmf.motion.Motion#toSession()
     */
    public List<MotionSession> toSessionList() {
        return new ArrayList<MotionSession>(mSessions);
    }

    /**
     *
     * @see jp.seraph.jsmf.motion.Motion#toTask()
     */
    public AgentTask toTask() {
        return new MotionTask(this);
    }

    public Iterator<MotionSession> iterator() {
        return mSessions.iterator();
    }
}
